﻿using System;
using System.Collections.Generic;
using System.Text;

using Kenly.ChineseSegment.DictionaryLoader;

namespace Kenly.ChineseSegment.Core
{
    public static class TextProcess
    {
        /// <summary>
        /// 根据指定的过滤字符过滤文本。
        /// </summary>
        /// <param name="text">待处理文本</param>
        /// <param name="filters">过滤字符</param>
        /// <returns></returns>
        public static string Filter(string text, List<string> filters)
        {
            return Filter(text, filters, false);
        }

        /// <summary>
        /// 根据指定的过滤字符过滤文本。
        /// </summary>
        /// <param name="text">待处理文本</param>
        /// <param name="filters">过滤字符</param>
        /// <param name="filterSpaces">是否过滤空格</param>
        /// <returns></returns>
        public static string Filter(string text,List<string> filters,bool filterSpaces)
        {
            if (string.IsNullOrEmpty(text))
            {
                return "";
            }
            if (filters == null || filters.Count <= 0)
            {
                return text;
            }

            StringBuilder tempBuilder = new StringBuilder(text);
            if (filterSpaces)
            {
                tempBuilder.Replace(" ", "");
            }

            foreach (string filter in filters)
            {
                tempBuilder.Replace(filter, "");
            }

            return tempBuilder.ToString();
        }

        /// <summary>
        /// 根据指定的过滤字符字典文件过滤文本。
        /// </summary>
        /// <param name="text">待处理文本</param>
        /// <param name="filterFilePath"></param>
        /// <param name="loader"></param>
        /// <param name="filterSpaces">是否过滤空格</param>
        /// <returns></returns>
        public static string Filter(string text, string filterFilePath, IDictionaryLoader loader, bool filterSpaces)
        {
            if (string.IsNullOrEmpty(filterFilePath))
            {
                return "";
            }
            if (loader == null)
            {
                return text;
            }

            List<string> filters = loader.Load(filterFilePath);
            return Filter(text, filters, filterSpaces);
        }

        /// <summary>
        /// 根据指定的过滤字符字典文件过滤文本。
        /// </summary>
        /// <param name="text">待处理文本</param>
        /// <param name="filterFilePath"></param>
        /// <param name="loader"></param>
        /// <returns></returns>
        public static string Filter(string text, string filterFilePath, IDictionaryLoader loader)
        {
            return Filter(text, filterFilePath, loader, false);
        }

        /// <summary>
        /// 按标点符号切分成句子。
        /// </summary>
        /// <param name="text">待处理文本</param>
        /// <returns></returns>
        public static string[] SplitSentences(string text)
        {
            List<string> spliters = new List<string>();
            spliters.Add(" ");
            spliters.Add("，");
            spliters.Add("。");
            spliters.Add("？");
            spliters.Add("：");
            spliters.Add("、");
            spliters.Add("；");
            spliters.Add("！");
            spliters.Add("\r\n");

            string[] sentences = text.Split(spliters.ToArray(), StringSplitOptions.RemoveEmptyEntries);
            return sentences;
        }
    }
}
